import constant from './constants'

export const handlemsg = (data, status, dispatch, onSucess) => {
    if (data.code !== undefined) {
        // 服务器响应错误
        dispatch.global.globalError(data.message)
    } else {
        // 未授权
        if (status === constant.STATUS_CODE_UNAUTH) {
            dispatch.global.globalError("授权已过期")
            dispatch.global.onAuthFailed()
        } else {
            // 业务逻辑错误
            if (data.err !== undefined || data.error !== undefined) {
                dispatch.global.globalError(data.error_description || constant.ErrorCode[data.err] || constant.ErrorCode[9999])
            } else {
                onSucess(data)
            }
        }
    }
}

export const handleResp = (resp, dispatch, callback) => {
    const {data, status} = resp
    // 请求成功 状态 200
    if (status === 200) {

        let err = data.err || data.error

        if (err) {
            dispatch.global.globalError(data.error_description || constant.ErrorCode[data.err] || constant.ErrorCode[9999])
            // dispatch.global.onAuthFailed()
        } else {
            callback(data)
        }
    } else {
        if (status === 401) {
            dispatch.global.globalError(data.error_description || constant.ErrorCode[data.err] || constant.ErrorCode[9999])
            dispatch.global.onAuthFailed()
        }
    }
}

export const compareArray = (arr1, arr2) => {

    const l1 = arr1.length
    const l2 = arr2.length
    if (l1 !== l2) return false

    for (let i = 0; i < l1; i++) {
        let e1 = arr1[i]
        let e2 = arr2[i]
        if (e1 !== e2) {
            return false
        }
    }

    return true

}

/**
 *
 * @param {随机字符串长度} len
 * @returns 随机字符串
 * @about number.toString(radix) // 转化成几进制
 */
export const RandomId = len => Math.random().toString(36).substr(3, len);

/**
 *
 * @param {需要的number} n
 * @returns n的整数部分
 */
export const ToInteger = n => n >> 0

/**
 *
 * @param {最小值} min
 * @param {最大值-不包含} max
 * @returns 随机整数
 */
export const RandomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;

/**
 * 可await
 * @param {延迟时间} ms
 */
export const delay = ms => new Promise(resolve => setTimeout(resolve, ms))

export const mustInputBeforeSubmit = (mapper, vals, emitError) => {

    for (let key in vals) {
        let val = vals[key]
        if (val === undefined || val === null || val === '') {
            emitError(`${mapper[key]}不能为空`)
            return false
        }
    }
    return true
}
export const getDom = selector => document.querySelector(selector)
export const getDoms = selector => document.querySelectorAll(selector)
export const bindEvent = (dom, action, callback) => dom.addEventListener(action, callback)

export const log = console.log.bind(console)

export const onSelected = (selectedItem, array, filterProp, updateProp, arrCallback, itemCallback,) => {
    const selectedIndex = array.findIndex(item => item[filterProp] === selectedItem[filterProp])
    const newArr = []
    array.map((g, i) => {
        g[updateProp] = i === selectedIndex
        newArr.push({...g})
    })
    arrCallback && arrCallback(newArr)
    itemCallback && itemCallback(selectedItem)
}
